#include <sys_config.h>
#include <types.h>
#include <basic_types.h>
#include <mediatypes.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <osal/osal.h>

#include <api/libosd/osd_lib.h>

#include "osdobjs_def.h"

#include "string.id"
#include "images.id"
#include "osd_config.h"

#include "win_com_menu_define.h"
#include "win_com.h"
#include "menus_root.h"
#if (AOV_OSD_DISPLAY_SETTING_ENABLE == SYS_FUNC_ON)
#ifdef DISPLAY_SETTING_SUPPORT

/*******************************************************************************
*   Objects definition
*******************************************************************************/
extern CONTAINER g_win_display_set;

extern CONTAINER dis_item_con1;
extern CONTAINER dis_item_con2;
extern CONTAINER dis_item_con3;
extern CONTAINER dis_item_con4;
extern CONTAINER dis_item_con5;
extern CONTAINER dis_item_con6;

extern TEXT_FIELD dis_item_txtname1;
extern TEXT_FIELD dis_item_txtname2;
extern TEXT_FIELD dis_item_txtname3;
extern TEXT_FIELD dis_item_txtname4;
extern TEXT_FIELD dis_item_txtname5;
extern TEXT_FIELD dis_item_txtname6;

extern MULTISEL dis_item_txtset1;
extern MULTISEL dis_item_txtset2;
extern MULTISEL dis_item_txtset3;
extern MULTISEL dis_item_txtset4;
extern MULTISEL dis_item_txtset5;
extern MULTISEL dis_item_txtset6;

extern TEXT_FIELD dis_item_line1;
extern TEXT_FIELD dis_item_line2;
extern TEXT_FIELD dis_item_line3;
extern TEXT_FIELD dis_item_line4;
extern TEXT_FIELD dis_item_line5;
extern TEXT_FIELD dis_item_line6;

static UINT32 m_dis_set_range[][3];

static VACTION dis_item_sel_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT dis_item_sel_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION dis_item_con_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT dis_item_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION dis_con_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT dis_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);


#define WIN_SH_IDX    WSTL_WIN_BODYRIGHT_01_HD

#define CON_SH_IDX   WSTL_BUTTON_01_HD
#define CON_HL_IDX   WSTL_BUTTON_05_HD
#define CON_SL_IDX   WSTL_BUTTON_01_HD
#define CON_GRY_IDX  WSTL_BUTTON_07_HD

#define TXT_SH_IDX   WSTL_BUTTON_01_FG_HD
#define TXT_HL_IDX   WSTL_BUTTON_02_FG_HD
#define TXT_SL_IDX   WSTL_BUTTON_01_FG_HD
#define TXT_GRY_IDX  WSTL_BUTTON_07_HD

#define SEL_SH_IDX   WSTL_BUTTON_01_FG_HD
#define SEL_HL_IDX   WSTL_BUTTON_04_HD
#define SEL_SL_IDX   WSTL_BUTTON_01_FG_HD
#define SEL_GRY_IDX  WSTL_BUTTON_07_HD


#define TXT_L_OF  	TXTN_L_OF
#define TXT_W  		TXTN_W
#define TXT_H		TXTN_H
#define TXT_T_OF	TXTN_T_OF

#define SEL_L_OF 	TXTS_L_OF
#define SEL_W  		TXTS_W
#define SEL_H		TXTS_H
#define SEL_T_OF	TXTS_T_OF

enum
{
    IDC_DIS_BRIGHT = 1,
    IDC_DIS_CONTRAST,
    IDC_DIS_SATURATION,
    IDC_DIS_HUE,
    IDC_DIS_SHARP,
    IDC_DIS_MAX,
};

#define MIN_BRIGHTNESS_VALUE    0
#define MAX_BRIGHTNESS_VALUE    100
#define MAX_BRIGHTNESS_NUM      101

#define LDEF_CON(root, varCon,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,conobj,focusID)     \
    DEF_CONTAINER(varCon,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, CON_SH_IDX,CON_HL_IDX,CON_SL_IDX,CON_GRY_IDX,   \
    dis_item_con_keymap,dis_item_con_callback,  \
    conobj, focusID,1)


#define LDEF_TXT(root,varTxt,nxtObj,l,t,w,h,resID)      \
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, TXT_SH_IDX,TXT_HL_IDX,TXT_SL_IDX,TXT_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,resID,NULL)

#define LDEF_MSEL(root, varNum, nxtObj, ID, l, t, w, h,style,cur,cnt,ptabl) \
    DEF_MULTISEL(varNum,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,ID,ID,ID,ID, l,t,w,h, SEL_SH_IDX,SEL_HL_IDX,SEL_SL_IDX,SEL_GRY_IDX,   \
    dis_item_sel_keymap,dis_item_sel_callback,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 20,0,style,ptabl,cur,cnt)

#define LDEF_MENU_ITEM(root,varCon,nxtObj,varTxt,varNum,varLine,ID,IDu,IDd,l,t,w,h,\
                        resID,style,cur,cnt,ptbl) \
    LDEF_CON(&root,varCon,nxtObj,ID,ID,ID,IDu,IDd,l,t,w,h,&varTxt,ID)   \
    LDEF_TXT(&varCon,varTxt,&varNum,l + TXT_L_OF,t + TXT_T_OF,TXT_W,TXT_H,resID)    \
    LDEF_MSEL(&varCon,varNum,NULL/*&varLine*/,ID, l + SEL_L_OF ,t + SEL_T_OF,SEL_W,SEL_H,style,cur,cnt,ptbl)\
	LDEF_LINE(&varCon,varLine,NULL,1,1,1,1,1,l + LINE_L_OF, t + LINE_T_OF,LINE_W,LINE_H,0,0)


#define LDEF_WIN(varCon,nxtObj,l,t,w,h,focusID)     \
    DEF_CONTAINER(varCon,NULL,NULL,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX,   \
    dis_con_keymap,dis_con_callback,  \
    nxtObj, focusID,0)


LDEF_MENU_ITEM(g_win_display_set,dis_item_con1, &dis_item_con2,dis_item_txtname1,dis_item_txtset1,dis_item_line1,1,IDC_DIS_SHARP,2,   \
                CON_L, CON_T + (CON_H + CON_GAP)*0,CON_W,CON_H, RS_SYSTEM_DISPLAY_SET_BRIGHTNESS,\
                STRING_NUMBER,0,MAX_BRIGHTNESS_NUM,m_dis_set_range[IDC_DIS_BRIGHT])

LDEF_MENU_ITEM(g_win_display_set,dis_item_con2, &dis_item_con3,dis_item_txtname2,dis_item_txtset2,dis_item_line2,2,1,3,    \
                CON_L, CON_T + (CON_H + CON_GAP)*1,CON_W,CON_H, RS_SYSTEM_DISPLAY_SET_CONTRAST,\
                STRING_NUMBER,0,MAX_BRIGHTNESS_NUM,m_dis_set_range[IDC_DIS_CONTRAST])

//#if (SYS_MAIN_BOARD >= BOARD_SB_S3602F_QFP_01V01 && SYS_MAIN_BOARD < BOARD_S3602F_MAX)
LDEF_MENU_ITEM(g_win_display_set,dis_item_con3, &dis_item_con4,dis_item_txtname3,dis_item_txtset3,dis_item_line3,3,2,4,    \
                CON_L, CON_T + (CON_H + CON_GAP)*2,CON_W,CON_H, RS_SYSTEM_DISPLAY_SET_SATURATION,\
                STRING_NUMBER,0,MAX_BRIGHTNESS_NUM,m_dis_set_range[IDC_DIS_SATURATION])
/*
#else
LDEF_MENU_ITEM(g_win_display_set,dis_item_con3, NULL,dis_item_txtname3,dis_item_txtset3,dis_item_line3,3,2,1,  \
                CON_L, CON_T + (CON_H + CON_GAP)*2,CON_W,CON_H, RS_SYSTEM_DISPLAY_SET_SATURATION,\
                STRING_NUMBER,0,MAX_BRIGHTNESS_NUM,m_dis_set_range[IDC_DIS_SATURATION])
#endif
*/

LDEF_MENU_ITEM(g_win_display_set,dis_item_con4, &dis_item_con5,dis_item_txtname4,dis_item_txtset4,dis_item_line4,4,3,5,    \
                CON_L, CON_T + (CON_H + CON_GAP)*3,CON_W,CON_H, RS_SYSTEM_DISPLAY_SET_HUE,\
                STRING_NUMBER,0,MAX_BRIGHTNESS_NUM,m_dis_set_range[IDC_DIS_HUE])

LDEF_MENU_ITEM(g_win_display_set,dis_item_con5, NULL,dis_item_txtname5,dis_item_txtset5,dis_item_line5,5,4,1,  \
                CON_L, CON_T + (CON_H + CON_GAP)*4,CON_W,CON_H, RS_SYSTEM_DISPLAY_SET_SHARPNESS,\
                STRING_NUMBER,0,11,m_dis_set_range[IDC_DIS_SHARP])

LDEF_WIN(g_win_display_set,&dis_item_con1,W_L, W_T, W_W, W_H, 1)


/*******************************************************************************
*   Local functions & variables define
*******************************************************************************/

CONTAINER* dis_items[] =
{
    &dis_item_con1,
    &dis_item_con2,
    &dis_item_con3,
    &dis_item_con4,
    &dis_item_con5,
};


MULTISEL* dis_item_set[] =
{
    &dis_item_txtset1,
    &dis_item_txtset2,
    &dis_item_txtset3,
    &dis_item_txtset4,
    &dis_item_txtset5,
};

static UINT32 m_dis_set_range[][3] =
{
    {0, 0, 0},
    {MIN_BRIGHTNESS_VALUE,MAX_BRIGHTNESS_VALUE,1},
    {MIN_BRIGHTNESS_VALUE,MAX_BRIGHTNESS_VALUE,1},
    {MIN_BRIGHTNESS_VALUE,MAX_BRIGHTNESS_VALUE,1},
    {MIN_BRIGHTNESS_VALUE,MAX_BRIGHTNESS_VALUE,1},
    {MIN_BRIGHTNESS_VALUE,10,1},
 };

void win_dis_load_setting(void);
void win_dis_init_items(void);


void win_dis_enter_key(UINT8 id);


/*******************************************************************************
*   key mapping and event callback definition
*******************************************************************************/
static VACTION dis_item_sel_keymap(POBJECT_HEAD pObj, UINT32 key)
{
    VACTION act = VACT_PASS;

    switch(key)
    {
    case V_KEY_LEFT:
        act = VACT_DECREASE;
        break;
    case V_KEY_RIGHT:
        act = VACT_INCREASE;
        break; 
    case V_KEY_ENTER:
        act = VACT_ENTER;
        break; 
    default:
        break;
    }

    return act; 
}

static PRESULT dis_item_sel_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
    PRESULT ret = PROC_PASS;
    VACTION unact;
    UINT8 bID;
    UINT32 sel;
    UINT16* Uni_str;
    SYSTEM_DATA* sys_data;
    OSD_RECT rect;
    COM_POP_LIST_PARAM_T param;

    bID = OSD_GetObjID(pObj);
    sys_data = sys_data_get();
    switch(event)
    {
    case EVN_PRE_CHANGE:
        sel = *((UINT32*)param1);
        break;
    case EVN_POST_CHANGE:
        sel = param1;
        switch(bID)
        {
        case IDC_DIS_BRIGHT:
            sys_data_set_brightness(sel);
            break;
        case IDC_DIS_CONTRAST:
            sys_data_set_contrast(sel);
            break;
        case IDC_DIS_SATURATION:
            sys_data_set_saturation(sel);
            break;
        case IDC_DIS_HUE:
            sys_data_set_hue(sel);
            break;
        case IDC_DIS_SHARP:
            sys_data_set_sharpness(sel);
            break;
        }
        
        break; 
    case EVN_REQUEST_STRING:
        sel = param1;
        Uni_str= (UINT16*)param2;
        switch(bID)
        {
        case IDC_DIS_BRIGHT:
            break;
        case IDC_DIS_CONTRAST:
            break;

//      case IDC_DIS_HUE:           break;
        case IDC_DIS_SHARP:
            break;
        }
        break;
        
    case EVN_UNKNOWN_ACTION:
        win_dis_enter_key(bID);
        
        break;
    }
    return ret;
}

static VACTION dis_item_con_keymap(POBJECT_HEAD pObj, UINT32 key)
{
    return VACT_PASS;
}

static PRESULT dis_item_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
    return PROC_PASS;
}

static VACTION dis_con_keymap(POBJECT_HEAD pObj, UINT32 key)
{
    VACTION act;
    
    switch(key)
    {
    case V_KEY_UP:
        act = VACT_CURSOR_UP;
        break;
    case V_KEY_DOWN:
        act = VACT_CURSOR_DOWN;
        break;      
    case V_KEY_EXIT:
    case V_KEY_MENU:
        act = VACT_CLOSE;
        break;
    default:    
        act = VACT_PASS;
        break;
    }

    return act;
}

static PRESULT dis_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
    PRESULT ret = PROC_PASS;
    VACTION unact;

    switch(event)
    {
    case EVN_PRE_OPEN:
        wincom_open_title((POBJECT_HEAD)&g_win_display_set,RS_SYSTEM_DISPLAY_SET, 0);
        win_dis_load_setting();
        win_dis_init_items();
        break;
    case EVN_PRE_CLOSE:
        sys_data_save(1);
        /* Make OSD not flickering */
        *((UINT32*)param2) &= ~C_CLOSE_CLRBACK_FLG; 
        break;
    default:
        break;
    }

    return ret; 
}


/*******************************************************************************
*   other functions  definition
*******************************************************************************/

void win_dis_init_items(void)
{
    static BOOL init = FALSE;
    POBJECT_HEAD first_item;
    CONTAINER* root = &g_win_display_set;

    if(!init)
    {       
        init = TRUE;
    }
}


void win_dis_load_setting(void)
{
    MULTISEL * msel;    
    SYSTEM_DATA* sys_data;
    UINT32 sel;
    
    sys_data = sys_data_get();
    

    /* Brightness */
    msel = dis_item_set[IDC_DIS_BRIGHT - 1];
    sel = sys_data->avset.brightness;
    OSD_SetMultiselSel(msel, sel);

    /* Contrast */
    msel = dis_item_set[IDC_DIS_CONTRAST - 1];
    sel = sys_data->avset.contrast;
    OSD_SetMultiselSel(msel, sel);

    /* Saturation */
    msel = dis_item_set[IDC_DIS_SATURATION - 1];
    sel = sys_data->avset.saturation;
    OSD_SetMultiselSel(msel, sel);

    /* Hue */
    msel = dis_item_set[IDC_DIS_HUE - 1];
    sel = sys_data->avset.hue;
    OSD_SetMultiselSel(msel, sel);
    
    /* Sharpness */
    msel = dis_item_set[IDC_DIS_SHARP - 1];
    sel = sys_data->avset.sharpness;
    OSD_SetMultiselSel(msel, sel);

#ifdef _S3281_
    CONTAINER* hueCon = dis_items[IDC_DIS_HUE - 1];
    OSD_SetAttr(hueCon,C_ATTR_HIDDEN);
    CONTAINER* sharpCon = dis_items[IDC_DIS_SHARP - 1];
    OSD_SetAttr(sharpCon,C_ATTR_HIDDEN);
#endif

}

UINT8 win_display_set_get_item_num(void)
{
    UINT8 ID = OSD_GetFocusID((POBJECT_HEAD)&g_win_display_set);

    if (ID == IDC_DIS_SHARP)
        return 11;
    return MAX_BRIGHTNESS_NUM;
}

PRESULT comlist_display_set_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
    UINT16 i,wTop;
    UINT8 id;
    OBJLIST* ol;
    UINT8 temp[4];
    SYSTEM_DATA* p_sys_data=sys_data_get();
    
    PRESULT cbRet = PROC_PASS;

    ol = (OBJLIST*)pObj;
    
    if(event==EVN_PRE_DRAW)
    {
        id = OSD_GetFocusID((POBJECT_HEAD)&g_win_display_set);
        switch(id)
        {
        case IDC_DIS_BRIGHT:
        case IDC_DIS_CONTRAST:
        case IDC_DIS_SATURATION:
        case IDC_DIS_HUE:
        case IDC_DIS_SHARP:
            wTop = OSD_GetObjListTop(ol);
            for(i=0;i<ol->wDep && (i+wTop)<ol->wCount;i++)
            {
                sprintf(temp,"%d", i+wTop+m_dis_set_range[id][0]);
                win_comlist_set_str(i + wTop,temp,NULL,0);
            }
            break;
        }
    }
    else if(event == EVN_POST_CHANGE)
        cbRet = PROC_LEAVE;

    return cbRet;
}


void win_dis_enter_key(UINT8 id)
{
    SYSTEM_DATA* p_sys_data=sys_data_get();
    UINT16 result;
    OSD_RECT rect;
    COM_POP_LIST_PARAM_T param;
    UINT16 sel = 0;
    POBJECT_HEAD pObj,pObjItem;
    MULTISEL* msel;

    msel = dis_item_set[id - 1];
    pObj = (POBJECT_HEAD)msel;
    pObjItem = (POBJECT_HEAD)dis_items[id - 1];

#if Maker_GUI_ON
	rect.uTop=Maker_GUI_SM_T;
	rect.uLeft=Maker_GUI_MM_L+(Maker_GUI_SM_W/2);
	rect.uHeight=300;
	rect.uWidth=(Maker_GUI_SM_W/2);
#else
    rect.uLeft  = CON_L+SEL_L_OF;
    rect.uWidth = SEL_W;
    rect.uTop = pObj->frame.uTop;
#endif
    param.selecttype = POP_LIST_SINGLESELECT;

    switch(id)
    {
    case IDC_DIS_BRIGHT:
        sel = p_sys_data->avset.brightness;
        break;
    case IDC_DIS_CONTRAST:
        sel = p_sys_data->avset.contrast;
        break;
    case IDC_DIS_SATURATION:
        sel = p_sys_data->avset.saturation;
        break;
    case IDC_DIS_HUE:
        sel = p_sys_data->avset.hue;
        break;
    case IDC_DIS_SHARP:
        sel = p_sys_data->avset.sharpness;
        break;
    }
#if Maker_GUI_ON
#else
#ifndef SD_UI
    rect.uHeight = 340;
#else
	rect.uHeight = 200;
#endif
#endif
    param.cur = sel - m_dis_set_range[id][0];
    sel = win_com_open_sub_list(POP_LIST_MENU_DISPLAY_SET,&rect,&param) + m_dis_set_range[id][0];

    OSD_SetMultiselSel(msel, sel);
    OSD_NotifyEvent(pObj,EVN_POST_CHANGE,(UINT32)sel, 0);
    OSD_TrackObject(pObjItem,C_DRAW_SIGN_EVN_FLG | C_UPDATE_ALL);

}

#endif
#endif
